package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.activity.client.ActivityFeignClient;
import com.atguigu.gmall.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author mqx
 * @date 2020-8-18 11:21:14
 */
@Controller
public class SeckillController {

    @Autowired
    private ActivityFeignClient activityFeignClient;
    // 编写用户访问秒杀列表的控制器！
    // http://activity.gmall.com/seckill.html

    @GetMapping("seckill.html")
    public String seckillIndex(Model model){
        // 获取返回结果集
        Result result = activityFeignClient.findAll();
        model.addAttribute("list",result.getData());
        // 后台需要存储一个list数据
        return "seckill/index";
    }

    // 展示商品详情控制器
    @GetMapping("seckill/{skuId}.html")
    public String getItem(@PathVariable Long skuId,Model model){
        // 获取商品详情数据
        Result result = activityFeignClient.getSeckillGoods(skuId);
        // 存储数据
        model.addAttribute("item",result.getData());
        return "seckill/item";
    }

    // 获取下单码成功之后要访问的控制器
    // window.location.href = '/seckill/queue.html?skuId='+this.skuId+'&skuIdStr='+skuIdStr
    @GetMapping("seckill/queue.html")
    public String queue(HttpServletRequest request,
                        @RequestParam("skuId") Long skuId,
                        @RequestParam(name = "skuIdStr") String skuIdStr){
//        String skuId = request.getParameter("skuId");
//        String skuIdStr = request.getParameter("skuIdStr");
        // 需要存储skuId，skuIdStr
        request.setAttribute("skuId",skuId);
        request.setAttribute("skuIdStr",skuIdStr);

        return "seckill/queue";
    }

    // 秒杀订单页面
    @GetMapping("seckill/trade.html")
    public String trade(Model model){
        // 获取数据
        Result<Map<String, Object>> result = activityFeignClient.trade();
        // 判断返回值的方法
        if (result.isOk()){
            // 数据应该在result.data
            model.addAllAttributes(result.getData());
            return "seckill/trade";
        }else{
            model.addAttribute("message",result.getMessage());
            return "seckill/fail";
        }
    }




}
